package cn.edu.gcu.giftems.utils.export;


import cn.edu.gcu.giftems.pojos.OrderItem;
import cn.edu.gcu.giftems.service.OrderItemService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-10-28
 * @see
 */
public class OrderItemExportUtils {

    public String export(OrderItemService OrderItemService)
    {
        List<OrderItem> OrderItemList=new ArrayList<OrderItem>();
        System.out.println(OrderItemService);
        OrderItemList=OrderItemService.getOrderItemDao().findByPage(0,OrderItemService.findCount());


        /*设置表头：对Excel每列取名
         *(必须根据你取的数据编写)
         */
        String []tableHeader={"订单单项编号","订单编号","礼品名称","数量","单价"};
        /*
         *下面的都可以拷贝不用编写
         */
        short cellNumber=(short)tableHeader.length;//表的列数
        HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个excel
        HSSFCell cell = null; //Excel的列
        HSSFRow row = null; //Excel的行
        HSSFFont font = workbook.createFont(); //设置字体
        HSSFSheet sheet = workbook.createSheet("订单单项信息"); //创建一个sheet
        HSSFHeader header = sheet.getHeader();//设置sheet的头
        try {
            /**
             *根据是否取出数据，设置header信息
             *
             */
            if (OrderItemList.size() < 1) {
                header.setCenter("查无资料");
            } else {
                header.setCenter("订单单项表");
                row = sheet.createRow(0);
                for (int k = 0; k < cellNumber; k++) {
                    cell = row.createCell(k);//创建第0行第k列
                    cell.setCellValue(tableHeader[k]);//设置第0行第k列的值
                    font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色.
                }
                /*
                 * // 给excel填充数据这里需要编写
                 *
                 */
                for (int i = 0; i < OrderItemList.size(); i++) {
                    OrderItem OrderItem = (OrderItem) OrderItemList.get(i);//获取OrderItem对象
                    row = sheet.createRow((short) (i + 1));//创建第i+1行
                    row.createCell(0).setCellValue(OrderItem.getId());
                    row.createCell(1).setCellValue(OrderItem.getOrderId());
                    row.createCell(2).setCellValue(OrderItem.getGiftName());
                    row.createCell(3).setCellValue(OrderItem.getQuantity());
                    row.createCell(4).setCellValue(OrderItem.getPurchasePrice());
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        /*
         *下面的可以不用编写，直接拷贝
         *
         */
        HttpServletResponse response = null;//创建一个HttpServletResponse对象
        OutputStream out = null;//创建一个输出流对象
        try {
            response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
            out = response.getOutputStream();//
            response.setCharacterEncoding("UTF-8");
            String headerStr ="订单单项信息-导出";
            response.setHeader("Content-disposition","attachment; filename*=UTF-8''"
                    + URLEncoder.encode(headerStr, "UTF-8")+".xls");//filename是下载的xls的名，建议最好用英文
            response.setContentType("application/force-download");//设置类型
            response.setHeader("Pragma","No-cache");//设置头
            response.setHeader("Cache-Control","no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            workbook.write(out);
            out.flush();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{

                if(out!=null){
                    out.close();
                }

            }catch(IOException e){
                e.printStackTrace();
            }

        }

        return null;
    }
}

